package com.yami.shop.common.util;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;
import java.util.Scanner;

public class CodeGenerator {

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }


    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/yami_shops?serverTimezone=UTC&characterEncoding=utf-8&autoReconnect=true&allowMultiQueries=true&useUnicode=true&useSSL=false", "root", "123456")
                .globalConfig(builder -> {
                    builder.author("kubilewang") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("F://daima"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.yami.shop") // 设置父包名
                           /* .moduleName(scanner("模块名")) // 设置父包模块名*/
                            .entity("bean.model")
                            .mapper("dao")
                            .xml("mapper")
                            .controller("admin.controller")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "F://daima")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(scanner("表名,多个英文逗号分割").split(",")) // 设置需要生成的表名
                            .addTablePrefix("t_", "c_")   // 去掉表前缀
                            .mapperBuilder().enableBaseResultMap().enableBaseColumnList();

                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}

